package cn.signalpha.modules.shop.service;

import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.log.exception.ServiceException;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.base.dto.NoticeDTO;
import cn.signalpha.modules.base.entity.enums.NoticeTypeEnum;
import cn.signalpha.modules.queue.enums.RedisDelayQueueEnum;
import cn.signalpha.modules.queue.utils.RedisDelayQueueUtil;
import cn.signalpha.modules.shop.dto.SellerApplyDTO;
import cn.signalpha.modules.shop.entity.Seller;
import cn.signalpha.modules.shop.entity.SellerApply;
import cn.signalpha.modules.shop.excel.SellerApplyExcel;
import cn.signalpha.modules.shop.mapper.SellerApplyMapper;
import cn.signalpha.modules.shop.vo.SellerApplyVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
@AllArgsConstructor
public class SellerApplyService extends ServiceImpl<SellerApplyMapper, SellerApply> {

	private final SellerService sellerService;
	private final SellerRoleService sellerRoleService;
	private final RedisDelayQueueUtil redisDelayQueueUtil;

	/**
	 * 自定义分页
	 */
    public IPage<SellerApplyVO> selectSellerApplyPage(IPage<SellerApplyVO> page, Map<String, Object> params) {
        return page.setRecords(baseMapper.selectSellerApplyPage(page, params));
    }

    /**
	 * 自定义导出
	 */
    public List<SellerApplyExcel> selectSellerApplyExcel(Map<String, Object> params) {
        return BeanUtil.copy(baseMapper.selectSellerApplyPage(null, params), SellerApplyExcel.class);
    }

	/**
	 * 自定义Wrapper
	 */
	public QueryWrapper<SellerApply> selectSellerApplyWrapper(Map<String, Object> params) {
		QueryWrapper<SellerApply> wrapper = Wrappers.query();
		wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("userId")), SellerApply::getUserId, params.get("userId"));
		wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("status")), SellerApply::getStatus, params.get("status"));
		wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("categoryId")), SellerApply::getCategoryId, params.get("categoryId"));
		wrapper.lambda().like(ObjectUtil.isNotEmpty(params.get("name")), SellerApply::getName, params.get("name"));
		wrapper.lambda().like(ObjectUtil.isNotEmpty(params.get("mobile")), SellerApply::getMobile, params.get("mobile"));
		wrapper.lambda().ge(ObjectUtil.isNotEmpty(params.get("createTimeBegin")), SellerApply::getCreateTime, params.get("createTimeBegin"));
		wrapper.lambda().le(ObjectUtil.isNotEmpty(params.get("createTimeEnd")), SellerApply::getCreateTime, params.get("createTimeEnd"));
		wrapper.lambda().orderByDesc(SellerApply::getId);
		return wrapper;
	}

	/**
	 * 审核
	 */
	@Transactional(rollbackFor = Exception.class)
	public void sellerApplyAudit(Long id, SellerApplyDTO.AuditDTO auditDTO) {
		SellerApply sellerApply = baseMapper.selectById(id);
		Assert.isNull(sellerApply, MsgConstant.OBJECT_NULL);
		Assert.isTrue(sellerApply.getStatus() == 0, MsgConstant.REPEAT_AUDIT);
		sellerApplyCheckout(sellerApply);

		updateById(SellerApply.builder().id(id).status(auditDTO.getStatus()).remark(auditDTO.getRemark()).build());
		if(auditDTO.getIsCreate() == 1 && auditDTO.getStatus() == 1){
			//生成真实商户
			Seller seller = Objects.requireNonNull(BeanUtil.copy(sellerApply, Seller.class));
			seller.setAccount(sellerApply.getMobile());
			sellerService.createSeller(seller);
		}

		//推送消息
		NoticeDTO noticeDTO = new NoticeDTO();
		noticeDTO.setTargetId(sellerApply.getId());
		noticeDTO.setType(NoticeTypeEnum.SELLER_AUDIT.getType());
		redisDelayQueueUtil.addDelayQueue(noticeDTO, 1, RedisDelayQueueEnum.NOTICE.getCode());
	}

	/**
	 * 申请审核校验
	 */
	public void sellerApplyCheckout(SellerApply sellerApply) {
		if(sellerService.countByParams(Kv.create().set("account", sellerApply.getMobile())) > 0){
			throw new ServiceException("申请手机号已存在");
		}
	}

}

